/* memrw.cpp
 *
 * try to maximize bandwidth by bombarding with memcpy
 *
 * alternate read and write to a memory range
 */

#include <omp.h>
#include "bench.h"
#include <assert.h>

#include <string.h>

#define ACCESSES 1000000
#define AI 0

void stride(int* src, int startindex, int endindex, long num_accesses, int64_t block_size)
{
    //printf("In thread %d, startindex %d, endindex %d, num_accesses %ld\n", omp_get_thread_num(), startindex, endindex, num_accesses);
    int sum = 0;
    int index = startindex;
    int block_len = block_size / sizeof(int);

    bool mode = 0;
    volatile int dest;
    for (long i = 0; i < num_accesses; i++)
    {
        // copy the block of memory
        if (mode)
        {
            for (int j = 0;j < block_len; j++)
                dest = src[index + j];
            sum += dest;
        }
        else
        {
            for (int j = 0;j < block_len; j++)
                src[index + j] = dest;
            sum += src[index];
        }


        index += block_len;
        if (index > endindex - block_len)
            index = startindex;

        mode = !mode;
    }
    
    finaldest[omp_get_thread_num()] = sum;

    return;
}
